package com.kedacom.emptyproject.map.widget

import android.content.Context
import android.support.constraint.ConstraintLayout
import android.support.design.widget.CoordinatorLayout
import android.util.AttributeSet
import com.kedacom.emptyproject.MainApplication

/**
 * @author GuanHaoran
 * @description:
 * @date :2020/10/16 5:26 PM
 */
class MyConstraintLayoutBehaviorView(
    context: Context,
    attributes: AttributeSet?,
    defStyle: Int = 0
) : ConstraintLayout(context, attributes, defStyle) {

    private var params: CoordinatorLayout.LayoutParams? = null

    constructor(ctx: Context, attributes: AttributeSet?) : this(
        context = ctx,
        attributes = attributes
    )

    constructor(ctx: Context) : this(context = ctx, attributes = null)


    fun setPoi(midOffset: Float, float: Float) {

        if (params == null)
            params = layoutParams as CoordinatorLayout.LayoutParams



        params?.apply {
            if (float <= midOffset) {
                marginStart = (dp2px(12f) / midOffset * float).toInt()
                marginEnd = (dp2px(12f) / midOffset * float).toInt()
            }
            layoutParams = this

        }

    }


    fun dp2px(dpValue: Float): Int {
        var scale = 0f
        if (scale == 0f) {
            scale = MainApplication.applicationContext.resources.displayMetrics.density
        }
        return (dpValue * scale + 0.5f).toInt()
        /**
         * return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
         * dpValue,
         * MyApplication.getApplication().getResources().getDisplayMetrics());
         */
    }
}

